package com.android.namecard.util;

import com.android.namecard.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/* 告定义Adapter，继承BaseAdapter */
public class NamecardViewAdapter extends BaseAdapter { 
    private Context _con;
    private String[] _items;
    private int[] _icons;
    /* 构造符 */
    public NamecardViewAdapter(Context con,String[] items,int[] icons) {
	    _con=con;
	    _items=items;
	    _icons=icons;
    }

    @Override
    public int getCount() {
    	return _items.length;
    }

    @Override
    public Object getItem(int arg0){
    	return _items[arg0];
    }

    @Override
    public long getItemId(int position){
    	return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
	    LayoutInflater factory = LayoutInflater.from(_con);
	    /* 使用grid.xml为每几个item的Layout */
	    View v = (View) factory.inflate(R.layout.namecard_grid_item, null);
	    /* 取得View */
	    ImageView iv = (ImageView) v.findViewById(R.id.grid_icon);
	    TextView tv = (TextView) v.findViewById(R.id.grid_text);
	    /* 设定显示的Image与文? */
	    iv.setImageResource(_icons[position]);
	    tv.setText(_items[position]);
	    return v;
    } 
} 

